package com.lz.football.module.youngTrain.item
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.liangzhong.events.YoungTrainEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.OtherGoodVo;
	import com.lz.football.module.base.IComponent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.LItem;

	/**
	 *球童item 
	 * @author lake
	 * 
	 */	
	public class PlayerChildItem extends LItem implements IComponent
	{
		/**
		 *显示时间 
		 */		
		public var txtTime 				:TextField ;
		/**
		 *球员动作mc 
		 * 5帧
		 */		
		public var playerAtMC 			:MovieClip;
		/**
		 *时间进度条 
		 */		
		public var timeProMC 			:MovieClip;
		/**
		 * 球员颜色mc
		 */		
		public var playerColorMc 		:MovieClip;
		/**
		 *剩余时间 秒 
		 */		
		private var leftTime			:int;
		/**
		 * 是否成长完毕
		 */		
		private var isGrowComplete		:Boolean = false;
		/**
		 *出售 获取mc 
		 */		
		private var getSaleMc			:MovieClip;
		/**
		 *加速 
		 */		
		private var growBgn			:SimpleButton;
		/**
		 * 获取
		 */		
		private var getBtn				:SimpleButton;
		/**
		 * 出售
		 */		
		private var saleBtn			:SimpleButton;
		/**
		 *球童颜色
		 * 灰 - 》 黑 
		 * 1 - 7 
		 */		
		private var color				:int = 1;
		
		public function PlayerChildItem()
		{
			super();
		}
		/**
		 *获取设置数据 
		 * @return 
		 * 
		 */		
		override public function get data():*
		{
			return super.data;
		}
		override public function set data(value:*):void
		{
			super.data = value;
			//删除定时
			TimerManager.instance.removeFunction(onTiming);
			//开始时间
			var growbegin :int = data.growbegin;
			//成长时间
			var grow	  :int = data.grow;
			//当前时间
			var curSeconds 		:Number = AppData.getInstance().time;
			//当前时间与开始时间间隔 秒
			var grapTime :Number = curSeconds - growbegin;
			//剩余时间
			leftTime = grow - grapTime;
			if(leftTime > 1)//还剩时间
			{
				isGrowComplete = false;
				timeProMC.visible = true;
				TimerManager.instance.addFunction(onTiming);
				txtTime.htmlText = DateUtil.formatShowHMS(leftTime,true);
				//成长时间
				var fIndex :int = 11 * ((grow-leftTime)/grow);
				timeProMC.gotoAndStop(fIndex + 1);
			}
			else
			{
				timeProMC.visible = false;
				isGrowComplete = true;
				txtTime.htmlText = "可收获";
			}
			//球童级别
			var pid :int = data.pid;
			var otherGood :OtherGoodVo = AppData.getInstance().goodsManager.getOtherById(pid) as OtherGoodVo;
			if(otherGood)
			{
				color = otherGood.value;
				playerColorMc.gotoAndStop(color);
			}
		}
		/**
		 *时间计时 
		 */		
		private function onTiming():void
		{
			leftTime --;
			if(leftTime <= 0)
			{
				isGrowComplete = true;
				timeProMC.visible = false;
				txtTime.htmlText = "可收获";
				TimerManager.instance.removeFunction(onTiming);
			}
			else
			{
				txtTime.htmlText = DateUtil.formatShowHMS(leftTime,true);
				//成长时间
				var grow	  :int = data.grow;
				var fIndex :int = 11 * ((grow-leftTime)/grow);
				timeProMC.gotoAndStop(fIndex + 1);
			}
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			playerAtMC 		= Reflection.createInstance("com.football.youngtrain.playerAtSkin");
			timeProMC 		= Reflection.createInstance("com.football.youngtrain.timeProSkin");
			playerColorMc 	= Reflection.createInstance("com.lz.football.youngtrain.PlayerColor");
			getSaleMc		= Reflection.createInstance("com.football.youngtrain.getsaleskin");
			growBgn			= Reflection.createInstance("com.football.youngtrain.growskin");
			txtTime = playerColorMc.txtTime;
			getBtn			= getSaleMc.getBtn;
			saleBtn			= getSaleMc.saleBtn;
			this.addChild(playerAtMC);
			this.addChild(timeProMC);
			this.addChild(playerColorMc);
			timeProMC.x = 8;
			timeProMC.y = 10;
			playerColorMc.x = -5;
			playerColorMc.y = -8;
			var ramIndex :int = Math.random() * 5;
			playerAtMC.gotoAndStop(ramIndex + 1);
			addEvtListener();
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER,rollOverHandler);
			this.addEventListener(MouseEvent.ROLL_OUT,rollOutHandler);
			growBgn.addEventListener(MouseEvent.CLICK,growClickHandler);
			getBtn.addEventListener(MouseEvent.CLICK,getClickHandler);
			saleBtn.addEventListener(MouseEvent.CLICK,saleClickHandler);
		}
		/**
		 * 移上
		 * @param e
		 * 
		 */		
		private function rollOverHandler(e :MouseEvent):void
		{
			if(isGrowComplete)
			{
				this.addChild(getSaleMc);
				getSaleMc.y = 10;
			}
			else
			{
				this.addChild(growBgn);
				growBgn.x = 15;
				growBgn.y = 20;
			}
		}
		/**
		 * 移出
		 * @param e
		 * 
		 */		
		private function rollOutHandler(e :MouseEvent):void
		{
			if(getSaleMc && getSaleMc.parent)
			{
				getSaleMc.parent.removeChild(getSaleMc);
			}
			if(growBgn && growBgn.parent)
			{
				growBgn.parent.removeChild(growBgn);
			}
		}
		/**
		 *加速 
		 * @param e
		 * 
		 */		
		private function growClickHandler(e :MouseEvent):void
		{
			var info :String = "您确定花费"+AppData.getInstance().youngTrainManager.getGrowAccPrice(color);
			info += "W欧元将成长时间清除吗？";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,growAlertHandler);
		}
		/**
		 * 加速 确认
		 * @param type
		 * 
		 */		
		private function growAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.GROW_ACC,data.index,true));
			}
		}
		/**
		 *收获
		 * @param e
		 * 
		 */		
		private function getClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.GET_PLAYER,[data.index],true));
		}
		/**
		 *出售
		 * @param e
		 * 
		 */		
		private function saleClickHandler(e :MouseEvent):void
		{
			AppData.getInstance().youngTrainManager.curSaleColor = color;
			var info :String = "出售球童可获得";
			info += "欧元，您确定出售吗？";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,saleAlertHandler);
		}
		/**
		 * 出售 确认
		 * @param type
		 * 
		 */		
		private function saleAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new YoungTrainEvent(YoungTrainEvent.SALE_PLAYER,data.index,true));
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			this.removeEventListener(MouseEvent.ROLL_OVER,rollOverHandler);
			this.removeEventListener(MouseEvent.ROLL_OUT,rollOutHandler);
			growBgn.removeEventListener(MouseEvent.CLICK,growClickHandler);
			getBtn.removeEventListener(MouseEvent.CLICK,getClickHandler);
			saleBtn.removeEventListener(MouseEvent.CLICK,saleClickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			TimerManager.instance.removeFunction(onTiming);
		}
	}
}